<?php
/**
 * Http 上传下载
 * 上线协议及约定：
 * 客户端上传参数
 * post:
 *  
 */
namespace Controller;
use Controller\CommonController;
class HttpController extends CommonController{
    public function test(){
        echo "c-t\n";
    }
    /**
     * 文件上传
     * @return string
     */
    public function upload(){
        // 上传数据错误
        if(!isset($this->request->files['data'])){
            return $this->ajaxReturnError(lang('UPLOAD_FIELD_ERROR'));
        }
        
        // 文件上传错误
        if($this->request->files['data']['error'] > 0){
            return $this->ajaxReturnError($this->request->files['data']['error']);
        }
        
        // 上传配置
        $upload_config = config('UPLOAD');
        $allow_exts = isset($upload_config['ALLOW_EXTS']) ? $upload_config['ALLOW_EXTS'] : ['jpg', 'jpeg', 'png', 'gif'];      // 允许上传的文件后缀
        $max_size = isset($upload_config['MAX_SIZE']) ? $upload_config['MAX_SIZE'] : 1024*1024*2;           // 最大上传
        
        // 文件后缀类型检测
        $file_name_info = explode('.', $this->post['fileName']);
        $file_ext = end($file_name_info);
        if(!in_array($file_ext, $allow_exts)){
            return $this->ajaxReturnError(lang('UPLOAD_FILE_EXT_ERROR'));
        }
        
        // 文件大小检测
        if($this->post['fileSize'] > $max_size){
            return $this->ajaxReturnError(lang('UPLOAD_MAX_SIZE_ERROR'));
        }
        
        // 分片上传
        if(array_key_exists('chunked', $this->post) && $this->post['chunked']){
            $result = Core()->logic('File')->chunkUpload($this->request->files['data'], $this->post);
        }else{
            $result = Core()->logic('File')->upload($this->request->files['data']);
        }
        
        if(!$result['status']){
            return $this->ajaxReturnError($result['msg']);
        }
        
        return $this->ajaxReturnSuccess($result['data']);
    }
}